﻿using M.ShopMachine.Domain;
using M.ShopMachine.Domain.Dto;
using M.ShopMachine.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace M.ShopMachine.Repository.Impl
{
    public class BaseDeviceRepository : BaseRepository<Device>, IDeviceRepository
    {
        public BaseDeviceRepository(EFDbContext eFDbcontext) : base(eFDbcontext)
        {
        }

        public async Task<int> AddDeviceAsync(Device device)
        {
            var id = await base.AddAsync(device);
            return id;
        }

        public async Task<List<Device>> GetShopMachineAsync(int id)
        {            
            var gather = await base.GetListAsync(x => x.WorkshopId == id && x.IsDeleted == false);
            if (id == 0)
            {
                gather = null;
                return null;
            }
            return gather.ToList();
        }

        public async Task<List<Device>> GetShopDeviceAsync(string? Number)
        {
            var gather = await base.GetListAsync(x => x.IsDeleted == false);
            if(Number != null)
            {
                gather = gather.Where(x=>x.DeviceNumber.Contains(Number));
            }
            return gather.ToList();
        }

        public async Task<List<Device>> GetTableDeviceAsync(string? Number, string? Name, bool? State)
        {
            var gather = await base.GetListAsync(x=>x.IsDeleted == false);
            var all = gather.ToList();
            if (Number != null)
            {
                gather = gather.Where(x=>x.DeviceNumber == Number);
            }
            if(Name != null)
            {
                gather = gather.Where(x => x.DeviceName.Contains(Name));
            }
            if(State != null)
            {
                gather = gather.Where(x => x.DeviceState == State);
            }

            return gather.ToList();
        }

        public async Task<int> UpdDeviceAsync(Device device)
        {
            var num = await base.UpdAsync(device);
            return num;
        }

        public async Task<int> UpdShopDeviceAsync(int WorkshopId, int[] DeviceIds)
        {
            var gather = await base.GetListAsync(x => x.IsDeleted == false && x.WorkshopId == WorkshopId);
            var gathers = gather.ToList();
            foreach (var device in gathers)
            {
                device.WorkshopId = 0;
                await base.UpdAsync(device);
            }
            var count = 0;
            if(DeviceIds.Length!=0)
            {
                foreach (var device in DeviceIds)
                {
                    var shopmachine = await base.GetAsync(x => x.Id == device);
                    shopmachine.WorkshopId = WorkshopId;
                    var num = await base.UpdAsync(shopmachine);
                    if (num != 0)
                    {
                        count++;
                    }
                }
            }
            
            return count;
        }

        public async Task<Device> GetFormDeviceAsync(int id)
        {
            var allor = await base.GetAsync(id);
            return allor;
        }

        public async Task<int> UpdStateDeviceAsync(int id, bool State)
        {
            var allor = await base.GetAsync(id);
            allor.DeviceState = State;
            var num = await base.UpdAsync(allor);
            return num;
        }
    }
}
